const config = require("../../config/index")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdown: 5,
    timer: null,
    webViewUrl: '',
    baseUrl: config.baseUrl,
    theme: wx.getStorageSync('InvoiceLoanTheme') || ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    // 设置倒计时定时器
    const timer = setInterval(() => {
      if (that.data.countdown <= 1) {
        clearInterval(timer);
        that.setData({ countdown: 0 });
        that.handleToAuth()
      } else {
        that.setData({ countdown: that.data.countdown - 1 });
      }
    }, 1000);
    that.setData({
      timer,
      webViewUrl: options.url,
      theme: wx.getStorageSync('InvoiceLoanTheme') || ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if(this.data.timer){
      clearInterval(this.data.timer)
    }
    this.setData({
      countdown: 0,
      timer: null
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if(this.data.timer){
      clearInterval(this.data.timer)
    }
  },
  handleToAuth(){
    const that = this
    wx.navigateTo({
      url: `/pages/otherWeb/index?url=${that.data.webViewUrl}`,
    })
  },
  handleBack(){
    wx.reLaunch({
      url: '/pages/productList/index',
    })
  }
})